<?php

/*------------------------------------------------
*
*   Explay Engine Content Management System.
*   Copyright © 2008 Golovdinov Alexander.
*
*-------------------------------------------------
*
*   Official site: wwww.alex-home.spb.ru/explay.html
*   Contact e-mail: golovdinov@gmail.com
*
*   GNU General Public License original source:
*   http://www.gnu.org/licenses/gpl-3.0.html
*
------------------------------------------------*/


if (!is_registered()) exit;

if (my_karma() < '-5') {
	alert ('У Вас слишком низкая карма, чтобы загружать изоображения!');
	exit;
}

include $_SERVER['DOCUMENT_ROOT'].'/engine/image.class.php';

$width = '';
$width_ = 450;

if (intval ($_POST['width']) != '' && intval ($_POST['width']) != 0 && intval ($_POST['width']) <= 400) {
	$width = " width=\"".intval ($_POST['width'])."\"";
	$width_ = intval ($_POST['width']);
}

if ($_POST['align'] == 'left')
	$align = ' align="left"';
elseif ($_POST['align'] == 'right')
	$align = ' align="right"';
elseif ($_POST['align'] == 'center')
	$align = ' align="center"';
else
	$align = '';

(strip_tags ($_POST['alt']) != '') ? $alt = strip_tags ($_POST['alt']) : $alt = '';


$prefix = time().'_';

if ($_FILES['image']['name'] != '') {
	if (eregi('[A-Za-z0-9]\.(jpeg|jpg|gif|png|bmp|JPEG|JPG|GIF|PNG|BMP)', $_FILES['image']['name'])) {
		if (copy($_FILES['image']['tmp_name'], $_SERVER['DOCUMENT_ROOT']."/include/cache/".$prefix.$_FILES['image']['name'])) {
			
			
			try {
				$image = new imageResize (
						$_SERVER['DOCUMENT_ROOT'].'/include/cache/'.$prefix.$_FILES['image']['name'],
						$prefix.User::$properties['user_id'],
						'images/articles',
						array($width_, 480),
						true,
						80
					);
				$image->resize ();
				
				print "<script type=\"text/javascript\">
					parent.document.record.big_text.value = parent.document.record.big_text.value + '<img src=\"/{$image}\"".$width.$alt.$align." />';
					parent.insert_text_img_close();
				</script>";
			}
			catch (Exception $e) {
				alert ($e->getMessage());
				exit;
			}
		}
		else alert ('Ошибка при добавлении изоображения!');
	}
	else alert ('Файл не является изоображением!');
}

function alert ($text) {
	print "<script type=\"text/javascript\">alert (\"$text\");</script>";
}

exit;

